package gui;

import java.util.concurrent.CountDownLatch;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class NewUserDialog extends Dialog {
	private static Display display;
	private static NewUserDialog inst;
	private static Shell shell;
	private static Shell dialogShell;
	private static Button logDialogButtonExit;
	private static Button logDialogButtonStart;
	private static Group logDialogGroup;
	private static Text logUserText;
	private static Text logPassText;
	public static boolean successful = false;

	static Image background = new Image(display,
			Main.class.getResourceAsStream("login-back.png"));

	public static void openNewUserDialog() {
		try {
			display = Display.getDefault();
			shell = new Shell(display);
			shell.setLocation(Main.getCentral_x() - 306,
					Main.getCentral_y() - 358);
			shell.setSize(900, 800);
			shell.setBackgroundImage(background);
			inst = new NewUserDialog(shell, 0);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeInitDialog() {
		shell.close();
	}

	public NewUserDialog(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, 67680);

			dialogShell.setText("Music Box - New User");
			dialogShell.setBackgroundImage(background);
			dialogShell.setLayout(null);

			logDialogGroup = new Group(dialogShell, 0);
			logDialogGroup.setLayout(null);
			logDialogGroup.setBackgroundImage(background);
			logDialogGroup.setBounds(0, -8, 390, 400);

			logUserText = new Text(logDialogGroup, 2048);
			logUserText.setBounds(180, 280, 125, 22);

			logPassText = new Text(logDialogGroup, 2048);
			logPassText.setBounds(180, 310, 125, 22);
			logPassText.setEchoChar('*');

			logDialogButtonStart = new Button(logDialogGroup, 16777224);

			logDialogButtonStart.setText("Create");
			logDialogButtonStart.setBounds(135, 350, 110, 40);
			logDialogButtonStart.addListener(13, new Listener() {
				public void handleEvent(Event event) {
					String user = NewUserDialog.logUserText.getText();
					String pass = NewUserDialog.logPassText.getText();

					if (user == "") {
						MessageBox messageBox = new MessageBox(
								NewUserDialog.shell, 296);

						messageBox.setMessage("Enter the User Name");
						messageBox.open();
					}
					if (pass == "") {
						MessageBox messageBox = new MessageBox(
								NewUserDialog.shell, 296);

						messageBox.setMessage("Enter the Password");
						messageBox.open();
					} else {
						Main.signal = new CountDownLatch(1);
						Main.dbGui.createNewUser(user, pass);

						try {
							Main.signal.await();
						} catch (InterruptedException e) {
							MessageBox messageBox = new MessageBox(
									NewUserDialog.shell, 32);
							messageBox.setText("Error");
							messageBox.setMessage("Error: "
									+ e.getLocalizedMessage());
							messageBox.open();
						}

						String guiErrorMsg = Main.guiInt.getMassage();
						System.out.println(guiErrorMsg + "login class");
						if (guiErrorMsg != null) {
							MessageBox messageBox = new MessageBox(
									NewUserDialog.shell, 32);
							messageBox.setText("Login Form");
							messageBox.setMessage("Error: " + guiErrorMsg);
							messageBox.open();
						} else {
							Main.setUser(Main.guiInt.getUser());
							MessageBox messageBox = new MessageBox(
									NewUserDialog.shell, 288);
							messageBox.setText("Login Form");
							messageBox.setMessage("Welcome: "
									+ NewUserDialog.logUserText.getText());
							messageBox.open();
							NewUserDialog.successful = true;
							NewUserDialog.shell.close();
						}
					}
				}
			});
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();

			while (!dialogShell.isDisposed())
				if (!display.readAndDispatch())
					display.sleep();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Shell getDialogShell() {
		return dialogShell;
	}

	public static void setDialogShell(Shell dialogShell) {
		dialogShell = dialogShell;
	}

	public static Button getInitDialogButtonExit() {
		return logDialogButtonExit;
	}

	public static void setInitDialogButtonExit(Button logDialogButtonExit) {
		logDialogButtonExit = logDialogButtonExit;
	}

	public static Button getInitDialogButtonStart() {
		return logDialogButtonStart;
	}

	public static void setInitDialogButtonStart(Button logDialogButtonStart) {
		logDialogButtonStart = logDialogButtonStart;
	}

	public static Group getInitDialogGroup() {
		return logDialogGroup;
	}

	public static void setInitDialogGroup(Group logDialogGroup) {
		logDialogGroup = logDialogGroup;
	}

	public static Display getInitDisplay() {
		return display;
	}

	public static void setDisplay(Display display) {
		display = display;
	}

	public static Display getDisplay() {
		return display;
	}

	public static NewUserDialog getInst() {
		return inst;
	}

	public static Shell getShell() {
		return shell;
	}
}